global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/table_A16_nickells_bias.log, replace name(tabl)

capture noi {


	qui do ${code_dir}/config/tabletools.do

	use ${final_dir}/regression_dataset_from1970_tfacit1.dta, clear
	tab year, gen(YY_)
	clonevar stockown = k${depvar}_${ttt} 
	clonevar stockownzero = k${depvar}_${ttt}0 
	clonevar stockother = kNOT_${depvar}_${ttt} 
	clonevar stockotherzero = kNOT_${depvar}_${ttt}0
	clonevar LSW = lswMPm_1995_a
	clonevar HSW = hswMPm_1995_a
	clonevar GDPGAP = lngdpgap_1995_a
	clonevar VAEMP = vaempMPm_1995_a
	clonevar spilloversown = spill${depvar}${ttt}_1995_a
	clonevar spilloversother = spillN${depvar}${ttt}_1995_a
	clonevar spilloversownzero = spill${depvar}${ttt}_1995_a0
	clonevar spilloversotherzero = spillN${depvar}${ttt}_1995_a0
	bys lse_id : egen _total_${depvar}_${ttt}_1995 = sum(${depvar}_${ttt}) if year>=1995+2 & year <= 2009+2
	bys lse_id : egen total_${depvar}_${ttt}_1995 = max(_total_${depvar}_${ttt}_1995)
	drop _total_${depvar}_${ttt}_1995

	gen firstyr=1995+1
	ge Kt1 = k${depvar}_${ttt} if year==firstyr
	bys lse_id : egen init_k${depvar}_${ttt}_1995=max(Kt1)
	ge dum_Kt1 = k${depvar}_${ttt}0 if year==firstyr
	bys lse_id : egen dum_init_k${depvar}_${ttt}_1995=max(dum_Kt1)
	drop Kt1 dum_Kt1
	drop firstyr 	

	sort lse_id year
	egen yearindustry = group(year industry) if year <= 2009
	egen yearctryindustry = group(year industry country_shr_1995) if year <= 2009
	egen yearctry = group(year country_shr_1995) if year <= 2009

	estimates clear 

	foreach vv in lswMPm hswMPm lngdpgap vaempMPm{
		bys lse_id : egen avg_`vv'_1995_a = mean(`vv'_1995_a) if year>=1995
	}
	foreach x of varlist spillover* {
		bys lse_id : egen avg_`x' = mean(`x') if year>=1995
	}

	*1) HHG | F + IY
	ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP VAEMP stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0, absorb(lse_id yearindustry) vce(cluster lse_id)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\no"
	estadd local estimator "HHG"
	hasvar stockown, local(stockown)
	hasvar stockother, local(stockother)
	hasvar spill*, local(spill)
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col1995_1

	*2) BGVR | F + IY
	ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP VAEMP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero *init* *avg*  if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0, absorb(yearindustry) vce(cluster lse_id)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\no"
	estadd local estimator "BGVR"
	hasvar stockown, local(stockown)
	hasvar stockother, local(stockother)
	hasvar spill*, local(spill)
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col1995_2

	*3) HHG | F + IY + CY
	ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP VAEMP stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0,absorb(lse_id yearindustry yearctry) vce(cluster lse_id)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local estimator "HHG"
	hasvar stockown, local(stockown)
	hasvar stockother, local(stockother)
	hasvar spill*, local(spill)
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col1995_3

	* 4) BGVR | F + IY + CY
	ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP VAEMP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero *init* *avg*  if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0,absorb(yearindustry yearctry) vce(cluster lse_id)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local estimator "BGVR"
	hasvar stockown, local(stockown)
	hasvar stockother, local(stockother)
	hasvar spill*, local(spill)
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col1995_4


	clonevar VAEMP_foreign = vaempMPm_shr4_foreign_1995_a
	clonevar GDPgap_foreign = lngdpgap_shr_foreign_1995_a
	clonevar LSW_foreign = lswMPm_shr4_foreign_1995_a
	clonevar HSW_foreign = hswMPm_shr4_foreign_1995_a

	drop avg_lsw avg_hsw avg_lngdp avg_vaemp
	*generate averages
	foreach vv in lswMPm_shr4_foreign hswMPm_shr4_foreign lngdpgap vaempMPm_shr4_foreign{
		bys lse_id : egen avg_`vv'_1995_a = mean(`vv'_1995_a) if year>=1995
	}
	*5) HHG | Foreign
	ppmlhdfe F2.${depvar}_${ttt} LSW_foreign HSW_foreign GDPgap_foreign VAEMP_foreign stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0,absorb(lse_id yearindustry yearctry) vce(cluster lse_id)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local estimator "HHG"
	hasvar stockown, local(stockown)
	hasvar stockother, local(stockother)
	hasvar spill*, local(spill)
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col1995_5

	*6) BGVR | Foreign
	ppmlhdfe F2.${depvar}_${ttt} LSW_foreign HSW_foreign GDPgap_foreign VAEMP_foreign stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero *init* *avg*  if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0,absorb(yearindustry yearctry) vce(cluster lse_id)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local estimator "BGVR"
	hasvar stockown, local(stockown)
	hasvar stockother, local(stockother)
	hasvar spill*, local(spill)
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col1995_6


	setlabels

	dhoztab * using ${tab_dir}/appendix/Table_A16_nickells_bias.tex,  b(%10.2f) se(%10.2f) nogaps replace numbers nonotes nolines nomtitles sfmt(a1) scalars("has_stockown \noalign{\medskip} Stock automation" "has_stockother Stock other" "has_spill Spillovers" "f Firm fixed effects" "iy Industry \stimes year fixed effects" "cy Country \stimes year fixed effects" "estimator Estimator" "obs \noalign{\medskip} Observations"  "firms Number of firms") label mgroups("Domestic and foreign" "Foreign", pattern(1 0 0 0 1 0) span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) drop(spill* stock* avg* *zero dum_* init_* _cons) noobs rename(LSW_foreign LSW HSW_foreign HSW GDPgap_foreign GDPGAP VAEMP_foreign VAEMP GDPPC_foreign GDPPC) ///
		depvar("Auto95") order(LSW HSW GDPGAP VAEMP stockown stockother spilloversown spilloversother) ///
		notes("This table addresses potential Nickell's bias. The coefficients are estimated with conditional Poisson regressions fixed-effects (HHG) in columns 1, 3, and 5. In columns 2, 4, and 6, the coefficients are estimated with Poisson regressions where the firm fixed effects are replaced by the pre-sample mean, following Blundell, Griffith and Van Reenen (1999, BGVR). All columns include firm and industry-year fixed effects. Columns 3--6 add country-year fixed effects. In In Columns 5 and 6 the macroeconomic variables are the normalized foreign variables as defined in the text. Standard errors are clustered at the firm-level and reported in parentheses.")
	*again, brackets work
}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}